<template>
	<view class="recharge-page">
		<u-navbar :autoBack="true" title="充值" :placeholder="true" bgColor="rgba(255,255,255,0)"></u-navbar>
		<view class="content">
			<view class="recharge">
				<view class="recharge-title">
					充值金额
				</view>
				<view class="amount">
					<view class="amount-text">￥</view>
					<u-input placeholder="请输入金额" border="none" @focus="focus" v-model="amount"></u-input>
				</view>
			</view>
			<view class="price">
				<view :class="item.show == true?'activity-item':''" class="price-item" v-for="(item,index) in priceList"
					:key="index" @click="cutPrice(index)">
					<view class="item-text">
						{{item.num}} <text>元</text>
					</view>
				</view>
			</view>
			<view class="mode">
				<view class="mode-title">
					支付方式
				</view>
				<view class="mode-item" v-for="(item,index) in payList" :key="index">
					<view class="mode-left">
						<image :src="item.img" mode=""></image>
						<view class="mode-text">
							{{item.text}}
						</view>
					</view>
					<u-radio-group v-model="radiovalue" @change="groupChange" iconPlacement="right">
						<u-radio :name="item.text" @change="handleChange(index)" shape="circle" size="24">
						</u-radio>
					</u-radio-group>
				</view>
			</view>
			<view class="btn">
				<u-button @click="gopay" shape="circle" size="large"
					color="linear-gradient( 90deg, #C58AF0 0%, #6272ED 100%)" type="primary" text="立即充值"></u-button>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				amount: '30',
				priceList: [{
						num: 30,
						show: true
					},
					{
						num: 50,
						show: false
					},
					{
						num: 100,
						show: false
					},
					{
						num: 300,
						show: false
					},
					{
						num: 500,
						show: false
					},
					{
						num: 1000,
						show: false
					}
				],
				payList: [{
						text: '微信',
						img: 'https://m.lqvvvv.com/assets/static/my/vx.png',
						checked: true,
					},
					// #ifdef APP
					{
						text: '支付宝',
						img: 'https://m.lqvvvv.com/assets/static/my/alipay.png',
						checked: false,
					},
					// #endif
					// {
					// 	text: '银行卡',
					// 	img: 'https://m.lqvvvv.com/assets/static/my/bank.png',
					// 	checked: false,
					// },
				],
				radiovalue: '微信',
				
			}
			
		},
		
		methods: {
			
			gopay() {
				if (this.amount == '') {
					uni.showToast({
						title: '充值金额不可为空！',
						icon: 'none'
					})
					return
				}
				uni.showLoading({
					mask: true
				})
				let params = {
					money: Number(this.amount).toFixed(2),
					paytype: '',
					// #ifdef APP
					method: 'app',
					// #endif
					// #ifdef H5
					method: 'web',
					// #endif
					// #ifdef MP-WEIXIN
					method: 'miniapp'
					// #endif
				}
				if (this.radiovalue == '微信') {
					params.paytype = 'wechat'
				} else {
					params.paytype = 'alipay'
				}
				this.$Request({
					method: 'POST',
					url: '/api/recharge/submit',
					data: params,
					ismsg: true,
				}).then(res => {
					
					uni.hideLoading()
					if (res.code == 1) {
						// #ifdef APP
						if(this.radiovalue == '微信'){
							let orderInfo = JSON.parse(res.data)
							console.log(orderInfo)
							uni.requestPayment({
								"provider": 'wxpay',
								"orderInfo":orderInfo,
								success: (res) => {
									console.log('支付成功', res);
									uni.showToast({
										title: '充值成功！',
										icon: 'none'
									})
									// 处理支付成功后的逻辑
								},
								fail: (err) => {
									uni.showToast({
										title: '支付失败！',
										icon: 'none'
									})
									console.error('支付失败', err);
									// 处理支付失败的逻辑
								},
							});
						}else{
							console.log(res)
							let orderInfo = res.data
							uni.requestPayment({
								"provider": 'alipay',
								...orderInfo,
								success: (res) => {
									console.log('支付成功', res);
									uni.showToast({
										title: '充值成功！',
										icon: 'none'
									})
									// 处理支付成功后的逻辑
								},
								fail: (err) => {
									uni.showToast({
										title: '支付失败！',
										icon: 'none'
									})
									console.error('支付失败', err);
									// 处理支付失败的逻辑
								},
							});
						}
						// #endif
						// #ifdef MP-WEIXIN 
						let info = res.data
						console.log(info)
						uni.requestPayment({
							"provider": 'wxpay',
							...info,
							success: (res) => {
								console.log('支付成功', res);
								uni.showToast({
									title: '充值成功！', 
									icon: 'none'
								})
								// 处理支付成功后的逻辑
							},
							fail: (err) => {
								uni.showToast({
									title: '支付失败！',
									icon: 'none'
								})
								console.error('支付失败', err);
								// 处理支付失败的逻辑
							},
						});
						// #endif
						
					}
				})

			},
			focus() {
				this.priceList.map(item => {
					item.show = false
				})
			},
			cutPrice(index) {
				this.amount = this.priceList[index].num
				this.priceList.map(item => {
					item.show = false
				})
				this.priceList[index].show = true
			},
			handleChange(index) {
				this.payList.map(item => {
					item.checked = false
				})
				this.payList[index].checked = true
			},
			groupChange(n) {
				console.log('groupChange', n);
			}
		}
	}
</script>

<style>
	page {
		background-color: #F7F8FC;
	}
</style>
<style lang="scss" scoped>
	.recharge-page {
		min-height: 100vh;
		background-image: url('https://m.lqvvvv.com/assets/static/index/nav-bg.png');
		background-repeat: no-repeat;
		background-size: 100% 800rpx;

		.content {
			padding: 30rpx;
			margin-top: 30rpx;

			.recharge {
				background-image: url('https://m.lqvvvv.com/assets/static/my/wallet-bg.png');
				background-repeat: no-repeat;
				background-size: 100% 100%;
				padding-bottom: 10rpx;

				.recharge-title {
					padding: 30rpx 30px 20rpx 30rpx;
					border-bottom: 1rpx solid #ffffff;
					font-size: 28rpx;
					font-weight: bold;
					color: #2E314A;
				}

				.amount {
					padding: 20rpx;
					margin: 30rpx;
					background-color: #EDEDF9;
					border-radius: 20rpx;
					display: flex;
					align-items: center;

					.amount-text {
						margin-right: 20rpx;
						font-weight: 600;
						font-size: 28rpx;
						color: #2E314A;
					}
				}
			}

			.price {
				display: flex;
				justify-content: space-between;
				flex-wrap: wrap;
				margin: 10rpx;

				.price-item {
					margin-top: 30rpx;
					border: 1rpx solid #C1C1C7;
					width: 30%;
					height: 130rpx;
					display: flex;
					justify-content: center;
					align-items: center;
					border-radius: 20rpx;
					background-color: #ffffff;
					color: #8B8DA0;

					.item-text {
						font-size: 32rpx;
						font-weight: bold;

						text {
							font-weight: normal;
							margin-left: 10rpx;
						}
					}
				}

				.activity-item {
					border: 2rpx solid #5755F2;
					background-color: #ECECFF;
					color: #5755F2;
				}
			}

			.mode {
				padding: 30rpx 40rpx;
				background-color: #ffffff;
				border-radius: 30rpx;
				margin-top: 50rpx;

				.mode-title {
					color: #000000;
					font-size: 28rpx;
					font-weight: bold;
					padding: 0 30rpx;
				}

				.mode-item {
					width: 100%;
					display: flex;
					align-items: center;
					justify-content: space-between;
					margin-top: 40rpx;
					padding: 0 20rpx;

					.mode-left {
						display: flex;
						align-items: center;

						image {
							width: 60rpx;
							height: 60rpx;
							border-radius: 50%;
						}

						.mode-text {
							width: 430rpx;
							font-size: 28rpx;
							color: #000000;
							font-weight: bold;
							margin-left: 30rpx;
						}
					}
				}
			}

			.btn {
				padding: 30rpx;
				margin-top: 30rpx;
			}
		}
	}
</style>